#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <stdio.h>
#include <unistd.h>
#include <sys/mman.h>
#include <stdlib.h>
#include <string.h>

int main()
{
int fd;
char *start_fp;
struct stat file_stat;

if ((fd = open("./file.txt", O_RDWR)) < 0)
{
printf("open file wrong!\n");
exit(1);
}

if ( fstat( fd, &file_stat) < 0 )
{
printf(" fstat wrong\n");
exit(1);
}

if ((start_fp = mmap(NULL, file_stat.st_size, PROT_READ | PROT_WRITE, MAP_SHARED, fd, 0 )) == MAP_FAILED)
{
printf("mmap wrong\n");
exit(0);
}

memcpy(start_fp, "test", 4);
if (munmap( start_fp, file_stat.st_size ) < 0 )
{
printf("munmap wrong\n");
exit(1);
}
close(fd);
}
